var prefix = ctx+ "/system/advertisement"
$(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		sbumitData();
	}
});

function closeIndex() {
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}

function sbumitData() {
    //判断修改也是否上传图片，没有上传图片则把oldAttachId值放到新的attachId中去
    var attachId = $("#attachId").val();
    var flag = judeImage(attachId);
    if(!flag) {
        $("#attachId").val($("#oldAttachId").val());
    }
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix+ "/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            advContent : {
                required : true,
                maxlength:2000
            },
            advCode : {
                required : true,
                number : true
            },
            advName : {
                required : true,
                maxlength:50
            }
        },
        messages : {
            advName : {
                required : icon  + "轮播位名称不能为空",
                maxlength: icon  + "最大长度不超过50"
            },
            advCode : {
                required : icon  + "轮播序号不能为空",
                number : icon  + "轮播序号为纯数字",
            },
            advContent : {
                required : icon  + "轮播内容不能为空",
                maxlength: icon  + "最大长度不超过2000"
            }
        }
    })
}

function judeImage(attachId) {
    var flag = false;
    $.ajax({
        type : "POST",
        url :  ctx + "/common/sysFile/list",
        data : {attachId:attachId},
        async : false,
        success : function(data) {
            if(data != null && data != '') {
                flag = true;
            }
        }
    });
    return flag;
}